home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4897 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.8 KB  |  92 lines

  1. Path: quadostimpy.natinst.com!user
  2. From: rcauvin@natinst.com (Roger L. Cauvin)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: should operator== and operator= be virtual?
  5. Date: 1 Feb 1996 15:02:06 GMT
  6. Organization: National Instruments
  7. Message-ID: <rcauvin-0102960906490001@quadostimpy.natinst.com>
  8. References: <4ejvkr$btb@itnews.sc.intel.com>
  9. NNTP-Posting-Host: quadostimpy.natinst.com
  10.  
  11. In article <4ejvkr$btb@itnews.sc.intel.com>, etse@scdt.intel.com (Eric
  12. Tse) wrote:
  13.  
  14. > Hi,
  15. > Should operator==() and operator=() of a class be virtual if the class is
  16. > planned for extensibility? Personally I think in general assignement 
  17. > operators should be non-virtual, but I am not sure about equality operator.
  18. > Could anyone helps? Thanks.
  19.  
  20. The following example illustrates the benefits of both virtual assignment
  21. operators and virtual comparison operators:
  22.  
  23. // Abstract string class.  Derived classes may use any strategy
  24. // they wish for character storage.
  25. class String
  26.    {
  27.    public:
  28.       virtual ~String(void) = 0;
  29.       virtual String & operator=(const char *cString) = 0;
  30.       virtual String & operator=(const String &string) = 0;
  31.       virtual bool operator==(const String &string) const = 0;
  32.    
  33.    protected:
  34.       String(void);
  35.    };
  36.  
  37. // Concrete static string class that uses a static array to
  38. // store its characters.
  39. class StaticString : public String
  40.    {
  41.    public:
  42.       StaticString(void);
  43.       virtual ~StaticString(void);
  44.       virtual String & operator=(const char *cString);
  45.       virtual String & operator=(const String &string);
  46.       virtual StaticString & operator=(const StaticString &staticString);
  47.       virtual bool operator==(const String &string) const = 0;
  48.    
  49.    private:
  50.       char staticBuffer[256];
  51.    };
  52.  
  53. // Concrete dynamic string class that uses a dynamic buffer
  54. // for storage of its characters.
  55. class DynamicString : public String
  56.    {
  57.    public:
  58.       DynamicString(void);
  59.       virtual ~DynamicString(void);
  60.       virtual String & operator=(const char *cString);
  61.       virtual String & operator=(const String &string);
  62.       virtual DynamicString & operator=(const DynamicString &dynamicString);
  63.       virtual bool operator==(const String &string) const = 0;
  64.    
  65.    private:
  66.       char *dynamicBuffer;
  67.    };
  68.  
  69. // Given a reference to string, set it equal to today's date.
  70. void GetDateString(String &dateString)
  71.    {
  72.   dateString = "Thursday, February 1, 1996";
  73.    }
  74.  
  75. // Given to string references, compare them for equality.
  76. bool CompareDateStrings(const String &dateString1, const String &dateString2)
  77.    {
  78.    return dateString1 ==dateString2;
  79.    }
  80.  
  81. A deep inheritance structure containing many abstract classes MUST use
  82. virtual assignment and comparison operators in order to be sufficiently
  83. polymorphic.
  84.  
  85. ---
  86.  
  87. Roger L. Cauvin
  88. rcauvin@natinst.com
  89. Software Engineer
  90. National Instruments
  91.